package net.keebook.webapp.bean;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import net.keebook.webapp.dao.KeeBookDao;
import net.keebook.webapp.dao.UserDao;
import net.keebook.webapp.entity.KeeBook;
import net.keebook.webapp.entity.User;
import net.keebook.webapp.model.SessionModel;

@ManagedBean
@RequestScoped
public class SessionBean {
	private SessionModel sessionModel;
	@Inject
	private UserDao userDao;
	@Inject
	private KeeBookDao keeBookDao;
	
	private final Map<String, Boolean> keeBookAccessCache = new HashMap<String, Boolean>();
	
	@PostConstruct
	public void init() {
		setSessionModel(SessionModel.getInstance(FacesContext.getCurrentInstance()));
	}
	
	public Boolean getInvitationPending() {
		return !"".equals(getSessionModel().getInvitationId());
	}
	
	public Boolean getLoggedIn() {
		return getSessionModel().getLoggedIn();
	}
	
	public Boolean getKeeBookSelected() {
		return getSessionModel().getKeeBookSelected();
	}
	
	public Boolean getKeeBookAccess(String requiredLevel) {
		if (!keeBookAccessCache.containsKey(requiredLevel)) {
			keeBookAccessCache.put(requiredLevel, getKeeBookDao().getKeeBookAccess(getSessionModel(), requiredLevel));
		}
		return keeBookAccessCache.get(requiredLevel);
	}
	
	public User getUser() {
		return getUserDao().get(getSessionModel().getUserId());
	}
	
	public KeeBook getKeeBook() {
		return getKeeBookDao().get(getSessionModel().getKeeBookId());
	}
	
	public Boolean getAdmin() {
		return getSessionModel().getAdmin();
	}

	public SessionModel getSessionModel() {
		return sessionModel;
	}

	public void setSessionModel(SessionModel sessionModel) {
		this.sessionModel = sessionModel;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public KeeBookDao getKeeBookDao() {
		return keeBookDao;
	}

	public void setKeeBookDao(KeeBookDao keeBookDao) {
		this.keeBookDao = keeBookDao;
	}
}
